<template>
    <div>
        <h2 style="color: gray;">修改用户</h2>
        <el-divider></el-divider>

        <!-- 上传表单 -->
        <el-form  :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
            <el-form-item label="头像" prop="portrait">
                <el-upload
                    class="avatar-uploader"
                    :action="`${UPLOADURL}upload`"
                    :show-file-list="false"
                    :on-success="handleMovieSuccess"
                    :before-upload="beforeMovieUpload"
                >
                    <img v-if="form.portrait" :src="form.portrait" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="昵称" prop="nickname">
                <el-input style="width:50%; height: 20%;" v-model="form.nickname" placeholder="请输入电影名称"></el-input>
            </el-form-item>
            <el-form-item label="性别" >
                <el-radio v-model="form.gender" label="女">小姐姐</el-radio>
                <el-radio v-model="form.gender" label="男">小哥哥</el-radio>
            </el-form-item>
            <el-form-item label="电话号码" prop="phone">
                <el-input style="width:50%; height: 20%;" v-model="form.phone" placeholder="请输入电话号码"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="upwd">
                <el-input style="width:50%; height: 20%;" v-model="form.upwd" placeholder="请设置密码"></el-input>
            </el-form-item>
            <el-form-item label="生日" prop="birthday">
                <el-date-picker
                v-model="form.birthday"
                value-format="yyyy/MM/dd"
                type="date"
                placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <!-- <el-form-item label="居住地区" prop="showingon">
                <div class="block">
                    <el-cascader
                        v-model="value"
                        :options="options"
                        :props="{ expandTrigger: 'hover' }"
                        @change="handleChange"></el-cascader>
                </div>
            </el-form-item>  -->
            <el-form-item>
                <el-button type="primary" @click="Onsubmit">立即添加</el-button>
                <el-button @click="resetForm('form')">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import httpApi from '@/http';
    export default {
        data(){
            return{
                // 表单内容
                form:{
                    nickname:'',
                    upwd:'',
                    phone:'',
                    gender:'',
                    birthday:'',
                    // region:'',
                    portrait:''
                },
                // 验证表单
                rules:{
                    nickname:[ { required: true, message: '必填', trigger: 'blur' }],
                    upwd:[ { required: true, message: '必填', trigger: 'blur' }],
                    phone:[ { required: true, message: '必填', trigger: 'blur' }],
                    gender:[ { required: true, message: '必填', trigger: 'blur' }],
                    birthday:[ { required: true, message: '必填', trigger: 'blur' }],
                    // region:[ { required: true, message: '必填', trigger: 'blur' }],
                    portrait:[ { required: true, message: '必填', trigger: 'blur' }],
                },
            }
        },
        methods:{ 
            handleMovieSuccess(res, file) {
                // console.log(res)
                // 将上传成功后的图片访问路径，存入form对象
                this.form.portrait = res.data
            },
            // 处理上传之前的格式大小验证
            beforeMovieUpload(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式或者png格式!');
                }
                if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
           /** 提交表单 */
            Onsubmit(){
                this.$refs['form'].validate(valid=>{
                    if (valid) {
                        console.log(this.form);
                        httpApi.UserApi.addUser(this.form).then(res =>{
                            if(res.data.code == '200'){
                                this.$message({
                                message: '恭喜你，用户添加成功',
                                type: 'success'
                                });
                                 // 跳转到用户列表页：/htlogin/userlist
                                 this.$router.push('/htlogin/userlist')
                            }                            
                        })
                    } else {
                        this.$message({
                            showClose: true,
                            message: '添加失败',
                            type: 'error'
                        });
                    }
                });
            },
        },
         /** 生命周期，挂载完毕后执行 */
        mounted(){
             
        }
    }
</script>

<style lang="scss" scoped>
.el-form-item__content{
    line-height:5px;
}
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    /* height: 150px; */
    display: block;
  }
</style>
